package utils;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;



import model.Userlog;
import dtos.UserlogDTO;


/**
 * 
 * @author Radulescu Cristina
 * @author Pop Loredana
 *
 */
@Stateless
@LocalBean
public class UserlogDAO implements UserlogDAORemote {

	@PersistenceContext
	EntityManager em;


	@EJB
	Conv conv;

	/**
	 * Default constructor.
	 */
	public UserlogDAO() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Method used to update date and start time of userlog
	 * @param userlog
	 * @param date
	 * @param start time
	 */
	@Override
	public void updateDate(UserlogDTO userlog, Date date, Time startTime) {
		try {
			UserlogDTO u=getUserlog(userlog.getId());
			u.setDate(date);
			u.setStarttime(startTime);
			em.merge(u);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Method used to get user log by id
	 * @param id
	 * @return user
	 */
	@Override
	public UserlogDTO getUserlog(int id) {
		return conv.fromEntityUserlog(em.find(Userlog.class, id));
	}
	
	/**
	 * Method used to update user log
	 * @param userlog
	 */
	@Override
    public void updateUserlog(UserlogDTO ud){
		Userlog t = em.find(Userlog.class, ud.getId());
		if (t == null) {
			Userlog t1 = conv.fromEntityUserlogDTO(ud);
			em.persist(t1);
		} else {
			t.setEndtime(ud.getEndtime());;
			em.merge(t);
		}

    }
	
	/**
	 * Method used to insert user log
	 * @param userlog
	 */
	@Override
	public void insertUserlog(UserlogDTO log) {
		Userlog t = em.find(Userlog.class, log.getId());
    	if (t==null) {
    		Userlog t1 = conv.fromEntityUserlogDTO(log);
    		em.persist(t1);
    	}
    	else {
    		em.merge(t);
    	}
	}
	
	/**
	 * Method used to delete user log
	 * @param userlog
	 */
	@Override
    public void deleteUserlog(UserlogDTO ud){
		Userlog r = em.find(Userlog.class, ud.getId());
        em.createQuery("delete from Userlog where id=:id").setParameter("id", r.getId()).executeUpdate();
	}

	/**
	 * Method used to get all user logs by id
	 * @param id
	 * @return list of userlogs
	 */
	@Override
	public List<UserlogDTO> getAllUserlogsByUserId(int id){
		List<UserlogDTO> resultUserlogs = new ArrayList<>();
    	CriteriaBuilder cb = em.getCriteriaBuilder();
    	CriteriaQuery<Userlog> cq = cb.createQuery(Userlog.class);
    	Root<Userlog> logs = cq.from(Userlog.class);
    	cq.select(logs);
    	TypedQuery<Userlog> q = em.createQuery(cq);
		List<Userlog> logsList = q.getResultList();
		for (Userlog u : logsList) {
			if(u.getUser().getId()==id)
			resultUserlogs.add(conv.fromEntityUserlog(u));
		}
    	return resultUserlogs;
	}
}
